using data.contracts;

namespace generator
{
    public static class NamingRules
    {
        public static string ToInterfaceName(string part) {
            return ToInterfaceName(BoardSyntax.ParsedPin(part));
        }

        public static string ToInterfaceName(Pin pin) {
            if (IsTypeGiven(pin)) {
                return BoardSyntax.ParsedType(pin.Type);
            }
            var nameSpace = "";
            if (pin.Board.Contains(".")) {
                nameSpace = pin.Board.SubstringUpToLast(".") + ".";
            }
            return nameSpace + "I" + pin.Board.SubstringAfterLast(".").FirstLetterToUppercase();
        }

        public static string ImplementationFor(Pin pin) {
            var interfaceName = ToInterfaceName(pin);
            var nameSpace = "";
            if (interfaceName.Contains(".")) {
                nameSpace = interfaceName.SubstringUpToLast(".") + ".";
            }
            return nameSpace + interfaceName.SubstringAfterLast(".").TrimStart('I');
        }

        public static string ToVariableName(Pin pin) {
            return pin.Board.SubstringAfterLast(".");
        }

        public static string ToVariableName(string part) {
            var pin = BoardSyntax.ParsedPin(part);
            return ToVariableName(pin);
        }

        public static bool IsTypeGiven(Pin pin) {
            return !string.IsNullOrWhiteSpace(pin.Type);
        }

        public static bool IsSpecialName(string part) {
            return ToVariableName(part).StartsWith("$");
        }

        public static bool IsSpecialName(Pin pin) {
            return pin.Board.StartsWith("$");
        }

        public static string ToSpecialVariableName(string bauteil) {
            var variableName = ToVariableName(bauteil);
            return "__" + variableName.TrimStart('$');
        }

        public static string ToSpecialVariableName(Pin pin) {
            return ToSpecialVariableName(pin.Board);
        }
    }
}